/*
 * Copyright 2012-2013 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.test.monitor;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class ServiceMonitor {
	
	
	@Pointcut("execution(* com..*Service.*(..))")
	private void anyMethod(){
		//定义一个共用切入点,
	}
	
	@Pointcut("execution(* com..*Service.test(..))")
	private void test(){
		//定义一个共用切入点,
	}

	
//	@AfterReturning("execution(* search..*Service.*(..))")
	@AfterReturning("anyMethod()")
	public void logServiceAccess(JoinPoint joinPoint) {
		System.out.println("方法结束: " + joinPoint);
	}
	
	@Before("anyMethod()")
	public void startLog(JoinPoint joinPoint) {
		System.out.println("方法开始: " + joinPoint);
	}
	
	
//	@After("anyMethod()")
//	public void after(){
//		System.out.println("最终通知");
//	}
	
	
	@AfterThrowing("anyMethod()")
	public void doAfterThrow(){
		System.out.println("异常通知");
	}
	
	
	
	@Around("test()")
	public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{
	//		// 调用方法的参数  
//        Object[] args = pjp.getArgs();  
//        // 调用的方法名  
//        String method = pjp.getSignature().getName();  
//        // 获取目标对象  
//        Object target = pjp.getTarget();  
        // 执行完方法的返回值：调用proceed()方法，就会触发切入点方法执行  
		System.out.println("进入环绕通知"+pjp);
		Object object = pjp.proceed();//执行该方法
		System.out.println("退出环绕通知");
		return object;
	}

}
